package lab03.test7;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class Test7 {
    public static void main(String[] args) {
        String path="D:\\book.zip";
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(path);

//            entries:复数
//            EntryIterator:输入迭代器
//            enumeration:枚举
            Enumeration entries = zipFile.entries();//得到所有的元素

            ZipEntry zipEntry = null;
            //遍历每个元素
            while (entries.hasMoreElements()){
                zipEntry = (ZipEntry) entries.nextElement();
                System.out.println(zipEntry.getName());

                //创建book文件夹
         File bookfile = new File("D:\\book");
               bookfile.mkdir();

                //字节流：读文件
                //zipFile.getInputStream获得输入流
                InputStream fileInputStream = zipFile.getInputStream(zipEntry);

                FileOutputStream fileOutputStream = new FileOutputStream(
                        new File("D:\\book\\" + zipEntry.getName()));
                byte[] data = new byte[1024];
                while ((fileInputStream.read(data)) != -1){

                    fileOutputStream.write(data);

                }
                //写文件
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    }

